////////////////////////////////////////////////////////////////////////////////
// Copyright 2006 CORE Solutions Limited
// All rights reserved.
// PROPRIETARY AND CONFIDENTIAL INFORMATION.
// Use is subject to license terms.
//
// FUNCTION ID      :   
// AUTHOR           :    
// DATE CREATE      :   
// DESCRIPTION      :   
//                  :   
//                  :   
//                  :   
//
// MODIFICATION HISTORY:
// DATE         AUTHOR          VERSION NO.         COMMENT
// -----------  --------------  ------------------  ----------------------------
// 2006/04/26   Ric Yik         1.0                 BugZilla #5535
// 2006/05/26   Cyris Liang     1.0                 Added for return 0 when the String is null
//
////////////////////////////////////////////////////////////////////////////////
package com.erp.app.common.util;

import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Iterator;

import org.apache.commons.beanutils.PropertyUtils;


import com.erp.app.common.ProjectConstants;
import com.erp.framework.types.CustomBigInteger;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.user.User;

public class ProjectCommonUtil extends CommonUtil {

    public static String idKeyToCode(String s) {
        s = toCode(s);
        return isEmpty(s) ? s : s.charAt(0) == '_' ? s.substring(1) : s;
    }

    public static boolean toBoolean(String genericYes) {
        return ProjectConstants.GENERIC_YES.equals(genericYes);
    }

    public static String toGenericYesNo(boolean expression) {
        return (expression) ? ProjectConstants.GENERIC_YES : ProjectConstants.GENERIC_NO;
    }

    public static boolean isBetween(int i, int from, int to) {
        return (i >= from && i <= to);
    }

    public static int toInt(Number number) {
        return toInt(number, 0);
    }

    public static int toInt(Number number, int valueWhenNull) {
        return (number == null) ? valueWhenNull : number.intValue();
    }

    public static String toDatePart(String datePart) {
        return (ProjectCommonUtil.isEmpty(datePart)) ? "0" : datePart;
    }

    public static String toYearPart(String yearPart) {
        String y = toDatePart(yearPart);
        return (y.length() == 2) ? "20" + y : y;
    }

    public static CustomBigInteger nvlCustomBigInteger(CustomBigInteger value, CustomBigInteger nvl) {
        return (value != null) ? value : nvl;
    }
    
    // Cyris Added for return 0 when the String is null
    // Code begin
    public static CustomBigInteger nvlCustomBigInteger(String value) {
    	value = (value != null) ? value : "0";
        return nvlCustomBigInteger(CustomBigInteger.customize(value), new CustomBigInteger(0));
    }
    // Code end
    
    public static CustomBigInteger nvlCustomBigInteger(CustomBigInteger value) {
        return nvlCustomBigInteger(value, new CustomBigInteger(0));
    }

    public static String getUserCompanyCode(User u) {
        return (u != null) ? u.getCompanyCode() : null;
    }

    public static void assertTrue(boolean condition, String message) {
        if (!condition) throw new RuntimeException("Assertion failed: " + message);
    }

    public static void assertNotNull(Object object, String message) {
        assertTrue(object != null, message);
    }

    public static Object getPseudoIndexedProperty(Object object, String property, int index) {
        try {
            return PropertyUtils.getProperty(object, property + index);
        } catch (IllegalAccessException e) {
            return null;
        } catch (InvocationTargetException e) {
            return null;
        } catch (NoSuchMethodException e) {
            return null;
        }
    }

    public static void setPseudoIndexedProperty(Object object, String property, int index, Object value) {
        try {
            PropertyUtils.setProperty(object, property + index, value);
        } catch (IllegalAccessException e) {
        } catch (InvocationTargetException e) {
        } catch (NoSuchMethodException e) {
        }
    }

}
